﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using JsonTest1.Net;
using JsonTest1.Configure;
using System.IO;
using JsonTest1.Data;
using Newtonsoft.Json;
using JsonTest1.Json;
using System.Collections.Generic;

namespace JsonTest1.NetEngine
{

    public class AlbumTracksEngine : HttpMessage
    {
       

        public AlbumTracksEngine()
        {
            base.init();
        }

        public void RAlbumTracks(Album album, string encoding)
        {
            //this.OnDataStreamGenerated += new Net.HttpDataStreamCalback(HttpDataStreamCalback);
          /*  String url = "id+url+image/license/json/?album_id=" + album.Id;
            BeginRequest(Constants.GET_API + url);*/

            //需要特殊处理
            if (album == null || encoding == null)
            {
                return;
            }

            if (album.Tracks != null)
                return ;

            Track[] tracks = null;
            List<Track> allTracks = new List<Track>();

            int currentPage = 1;

          /*  while ((tracks = RAlbumTracks(album, encoding, TRACKS_PER_PAGE, currentPage++)) != null)
            {
                allTracks.addAll(Arrays.asList(tracks));
            }

            return allTracks.ToArray(new Track[0]);*/

        }


        public void RAlbumTracks(Album album, string encoding, int count, int page)
        {
            string pagination = "&n=all";
            if (count != 0 && page != 0)
            {
                pagination = "&n=" + count + "&pn=" + page;
            }
            string url = "numalbum+id+name+duration+rating+url+stream/track/json/?album_id=" + album.Id + "&streamencoding=" + encoding + pagination;
            BeginRequest(Constants.GET_API + url);
        }




        public override  bool HttpGeneratorDataCallback(Stream stream){

            try
            {
                //解析json,过滤错误
                string jsonStr = new StreamReader(stream).ReadToEnd();
                if (jsonStr == null)
                {
                    //AlbumFunctions.getAlbums();
                    HttpFailCalback(1, 1, new ResultData(1, 1, ""));
                }
                else
                {
                    if (httpStateListener != null)
                        httpStateListener.HttpDataStreamCalback(stream, new ResultData(0, 0, TracksFunctions.getTracks(jsonStr,true)));
                }
            }
            catch (JsonException e)
            {
                HttpFailCalback(1, 1, new ResultData(1, 1, ""));
            }
            catch (Exception e)
            {

            }

            return true;
        }
    }
}
